function audioSignal = Karplus_Strong(frequency , duration , ksData, Fs)
%ksData:       
%       sustain: de 0 a 1
%       randomness : 'gaussian', 'uniform'
%       volume: de 0 a 1
%       instrument: 'acoustic_guitar_piezo', 'acoustic_guitar_mic',
%       'banjo_piezo', 'banjo_mic', 'dobro_piezo', 'dobro_mic'
%       pickPosition: 0 a 1
%       pickDirection: 0 a 1
%       attack: 0 a 1(default)
    ksData.attack=(1-ksData.attack)*0.996;
    ksData.pickDirection=ksData.pickDirection*0.999;

    x = zeros(round(Fs*duration), 1);
    delay = round(Fs/frequency);
    if strcmp(ksData.randomness,'gaussian')
        zi=randn(delay,1)*2-1;
    else
        zi=rand(delay,1)*2-1;
    end
    
    x=[zi;x(1:end-length(zi))];
    x(1)=x(1)*(1-ksData.pickDirection);
    x=filter([1-ksData.pickDirection],[1 ksData.pickDirection],x);
    x=filter([1 zeros(1,round(ksData.pickPosition*delay))],[1],x);
    
    x2=(1-ksData.attack)*x(1);
    for k=2:length(x)
        x2(k)=x(k)*(1-ksData.attack)+ksData.attack*x2(k-1);
    end
    
    
    y=x2;
    for k=delay+2:length(x)
        y(k) = x2(k)+ (y(k-delay)+y(k-delay-1))/2;
    end
    note=y;
    note = note-mean(note);
    note=note(delay+1:end);
  
    %note(1:delay)=zeros(delay,1);%note(1:delay)*ksData.attack;
%     for k=1:length(note)
%         note(k)=note(k)*(1-exp(-k*200/Fs))^(10000*(1-ksData.attack));
%     end
    
   
    for k=1:length(note)
        note(k)=note(k)*exp(-(1-ksData.sustain)*5*k/Fs);
    end
%     for k=1:length(note)
%         note(k)=note(k)*(1-exp(-0.000001*k/Fs));
%     end
    audioSignal=note;

    
    audioSignal=audioSignal/max(abs(audioSignal));
    
    audioSignal=audioSignal';
end

%     F = linspace(1/Fs, 1000, 2^12);
%     b  = firls(42, [0 1/delay 2/delay 1], [0 0 1 1]);
%     a  = [1 zeros(1, delay) -0.5 -0.5];
%     [H,W] = freqz(b, a, F, Fs);%para graficar
%     
%     if strcmp(ksData.randomness,'gaussian')
%         zi = randn(max(length(b),length(a))-1,1);
%     else
%         zi = rand(max(length(b),length(a))-1,1);
%     end
%     
%     note = filter(b, a, x, zi);